package com.bookyourtrek.action;

import java.sql.SQLException;
import java.util.Map;

import com.bookyourtrek.model.LoginModel;
import com.bookyourtrek.util.UtilityFunctions;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAccountAction extends ActionSupport{

	/**
	 * 
	 */
	private String email;
	private String phone;
	private String password;
	
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}



	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	private static final long serialVersionUID = 1L;
	
	@SuppressWarnings("rawtypes")
	public String useraccount() throws SQLException {
		
		//get email from session
		Map sess = (Map) ActionContext.getContext().get("session");
		email =  (String) sess.get("useremail");
		
		//get user phone
		LoginModel loginmodel = new LoginModel();
		phone = loginmodel.getPhone((String) sess.get("useremail"));
		
		return SUCCESS;
		
	}
	
	public String updateuseraccount() {

		//update user account
		LoginModel loginmodel = new LoginModel();
		loginmodel.updateAccount(email, phone);
		
		//send email about changes
		
		return SUCCESS;
		
	}
	
	public String updateuserpassword() throws SQLException {

		//update user password
		LoginModel loginmodel = new LoginModel();
		loginmodel.updatePassword(email, UtilityFunctions.generateMD5(password));
		
		//send email about changes
		LogoutAction logoutaction = new LogoutAction();
		logoutaction.logout();
		return SUCCESS;
		
	}

}
